package adc.budget.ui.web.wicket.transaction;

import java.util.Date;
import java.util.List;

import org.apache.wicket.datetime.PatternDateConverter;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SimpleFormComponentLabel;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.util.convert.IConverter;

import adc.app.ui.wicket.BoEditPanel;
import adc.budget.spec.bo.ISource;
import adc.budget.spec.bo.ITransaction;
import adc.budget.ui.web.wicket.BudgetApplication;
import adc.budget.ui.web.wicket.IMsgsMisc;
import static adc.app.spec.util.AppUtil.anyCast;
public class TransactionEditPanel extends BoEditPanel<ITransaction> {

    private static final long serialVersionUID = 1L;

    public TransactionEditPanel(String id) {
        super(id);
    }

    @Override
    protected void addFields(Form<ITransaction> form) {
        TextField<String> tfSummary = new TextField<String>("summary");
        tfSummary.setLabel(new ResourceModel(IMsgsMisc.SUMMARY));
        form.add(new SimpleFormComponentLabel("summaryLbl", tfSummary)).add(tfSummary);
        
        TextField<String> tfReference = new TextField<String>("reference");
        tfReference.setLabel(new ResourceModel(IMsgsMisc.REFERENCE));
        form.add(new SimpleFormComponentLabel("referenceLbl", tfReference)).add(tfReference);
        
        TextArea<String> taNotes = new TextArea<String>("notes");
        taNotes.setLabel(new ResourceModel(IMsgsMisc.NOTES));
        form.add(new SimpleFormComponentLabel("notesLbl", taNotes)).add(taNotes);
        
        TextField<Date> tfDate = new TextField<Date>("date") {

            private static final long serialVersionUID = 1L;

            @Override
            public <C> IConverter<C> getConverter(Class<C> type) {
                return anyCast(new PatternDateConverter("yyyy-MM-dd HH:mm", false));
            }

        };
        tfDate.setLabel(new ResourceModel(IMsgsMisc.DATE));
        form.add(new SimpleFormComponentLabel("dateLbl", tfDate)).add(tfDate);
        
        TextField<Double> tfAmount = new TextField<Double>("amount");
        tfAmount.setLabel(new ResourceModel(IMsgsMisc.AMOUNT));
        form.add(new SimpleFormComponentLabel("amountLbl", tfAmount)).add(tfAmount);
        
        DropDownChoice<ISource> ddlSource = new DropDownChoice<ISource>("source",
                new SourcesModel(), new ChoiceRenderer<ISource>("name", "id"));
        ddlSource.setLabel(new ResourceModel(IMsgsMisc.SOURCE));
        form.add(new SimpleFormComponentLabel("sourceLbl", ddlSource)).add(ddlSource);
        
        DropDownChoice<ISource> ddlDestination = new DropDownChoice<ISource>("destination",
                new DestinationsModel(), new ChoiceRenderer<ISource>("name", "id"));
        ddlDestination.setLabel(new ResourceModel(IMsgsMisc.DESTINATION));
        form.add(new SimpleFormComponentLabel("destinationLbl", ddlDestination)).add(ddlDestination);
    }

    private static final class SourcesModel extends LoadableDetachableModel<List<ISource>> {

        private static final long serialVersionUID = 1L;

        @Override
        protected List<ISource> load() {
            return BudgetApplication.get().getSourceBs().getSources();
        }

    }

    private static final class DestinationsModel extends LoadableDetachableModel<List<ISource>> {

        private static final long serialVersionUID = 1L;

        @Override
        protected List<ISource> load() {
            return BudgetApplication.get().getSourceBs().getDestinations();
        }
        
    }
}
